Array
and KeyedLinkedList
are two of the Collection
classes from the ScriptX core classes. The Collection
classes implement a common set of methods for operations on the items they store.The ScriptX language provides access to members of collections with the element access expression. The element access expression provides a reference to an element by its position or key.
collection[position]In the element access expression, collection must evaluate to an instance of
collection[key]
Collection
such as Array
or KeyedLinkedList
. position is an ordinal position within the array, and key is a key in a key-value pair.Like the keys in the keyed linked list literal, you can specify complex expressions as the collection part of the element access expression by surrounding them with parentheses. You can also specify that the following expressions are the array or keyedLinkedList:
#(1, 2, 3)
)
myArray[1]
)
myRect.x1
, described in Chapter 3, "Working with Objects")
nextMethod
(described in Chapter 6, "Defining Classes and Objects)
empty
object. empty
is used specifically in the collection classes to mean "element not found."myMenu := #(@breakfast:"coffee", @lunch:"burrito",@dinner:"pizza")
#(@breakfast:"coffee", @lunch:"burrito", @dinner:"pizza")
myMenu[@midnightSnack]
empty
The element access expression is translated by the compiler into a call to the generic function getOne
. The getOne
generic function is implemented by all of the collection classes, and as such, the element access expression is also available to any collection. See Chapter 7, "Collections," for more information about the collection classes.
These examples show how to use the element access expression:
array1 := #( "un", "deux", "trois" )
array1[1]
"un"
array1[3]
"trois"
array1[0]
empty
#( "once", "twice", "thrice", "four times")[4]
"four times"
keyedList1 := #("snake":"cobra","bird":"sparrow","fish":"guppy")
keyedList1["bird"]
"sparrow"
array2 := #(array1)
array2[1]
#( "un", "deux", "trois" )
array2[1][2]
"deux"
(if array1.size > 1 then array1 else #(undefined))[1]
"un"
collection[position ] := valueAs in the element access expression, collection is a factor or expression within parentheses that results in a collection such as an array or keyed linked list. The position represents an ordinal position within the collection, while the key represents a key associated with one item. The value is either the value for the given key or the value to be placed at that position in the array.
collection[key] := value
The element access expression, when used with an assignment operator, is compiled into a call to the generic function setOne
. The setOne
generic is implemented by all collection classes, and as such, the element access construct is available to any collection.
These examples show how to change or add an element to a collection such as an array using the element access and assignment expressions:
global houseplant := #(@palm) -- create an array
#(@palm)
houseplant[2] := @africanViolet -- add a second element
@africanViolet
housePlant
#(@palm,@africanViolet)
houseplant[3] := @begonia -- add a third element
@begonia
housePlant
#(@palm,@africanViolet,@begonia)
houseplant[1] := @fern -- set a new value for first element
@fern
housePlant
#(@fern,@africanViolet,@begonia)
getOne
or setOne
, it can be used with any class that implements these generic functions. In the ScriptX core classes, many of the media classes are implemented as collections, including windows, menus, documents, animation lists, and even title containers. The element access expression is used with all of these classes, and with scripted collection classes as well.contains
:collection contains thingn this syntax, collection is an expression that evaluates to a
Collection
object, and thing is the object in the collection for which you are testing. The contains
expression returns either true
or false
, depending on whether or not the element is present in the collection.
The contains
construct is compiled into a call to the generic function isMember
before the expression is evaluated. The isMember
generic function is implemented by all collection classes, and as such, the contains
construct is available with any collection.
Here are some examples of how contains
is used in scripts:
x := #("one", "three", "five")
x contains "one"
true
x contains "two"
false
#(1:"one",2:"two",3:"three") contains "two"
true
This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.